<?php

class Lot extends Eloquent implements CrudableInterface
{
    public function group() {
        return $this->belongsTo('Group');
    }

    public function items() {
        return $this->hasMany('Item');
    }

    public function itemType() {
        return $this->belongsTo('ItemType');
    }

    public function container() {
        return $this->belongsTo('Container');
    }


    public static function indexColumns()
    {
        return array (
            'ID' => array (
                'type'=>'numeric',
                'field'=>'id'
            ),
            'Type' => array (
                'type'=>'string',
                'field'=>function ($obj) { return $obj->itemType ? $obj->itemType->name : ''; }
            ),                
            'Container' => array (
                'type'=>'string',
                'field'=>function ($obj) { return $obj->container ? $obj->container->name : ''; }
            ),                
            'Product Date' => array (
                'type'=>'date',
                'field'=>function ($obj) { return date('m/d/y', strtotime($obj->product_date)); }
            ),
            'Lbs per Item' => array (
                'type'=>'numeric',
                'field'=>'pounds_per_item'
            ),
            'Qty in Stock' => array (
                'type'=>'numeric',
                'field'=>function ($obj) { return $obj->items()->where('status', '=', 'in stock')->count(); }
            ),
            'Lbs in Stock' => array (
                'type'=>'numeric',
                'field'=>function ($obj) { return $obj->items()->where('status', '=', 'in stock')->count() * $obj->pounds_per_item; }
            ),

            'Item #s' => array (
                'type'=>'numeric',
                'field'=>function ($obj) { return $obj->items->min('item_number') . '-' . $obj->items->max('item_number'); }
            ),
            
        );
    }

    public function showTitle() 
    {
        return 'Lot';
    }

    public function showFields()
    {
        $numbers = $this->items->min('item_number') . '-' . $this->items->max('item_number');

        return array (
            'ID' => $this->id,
            'Type' => $this->itemType ? $this->itemType->name : 'unknown',
            'Container' => $this->container ? $this->container->name : 'unknown',
            'Product Date' => date ('m/d/y', strtotime ($this->product_date)),
            'Lbs per Item' => $this->pounds_per_item,
            'Item Numbers' => $numbers,
        );
    }

    public static function editFields($obj=null)
    {
       $item_types = ItemType::whereHas('group', function($q) {
                $q->where('group_id', '=', Auth::user()->group_id);
            })->get()->lists('name', 'id');

       $containers = Container::whereHas('group', function($q) {
                $q->where('group_id', '=', Auth::user()->group_id);
            })->get()->lists('name', 'id');

        return array (
            'item_type_id' => array (
                'type'=>'select',
                'options'=>$item_types,
                'label'=>'Type',
                'value'=> ($obj && $obj->itemType) ? $obj->itemType->id : '',
            ),                
            'container_id' => array (
                'type'=>'select',
                'options'=>$containers,
                'label'=>'Container',
                'value'=> ($obj && $obj->container) ? $obj->container->id : '',
            ),

            'product_date' => array (
                'type'=>'date',
                'label'=>'Product Date'
            ),
            'pounds_per_item' => array (
                'type'=>'numeric',
                'label'=>'Lbs per Item'
            ),
        );
        
    }
}